﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Engine
{
    /// <summary>
    /// Era para la tarea del joystick pero la dejo en caso de que sirva.
    /// Tendria que ser tipo interfaz para que otros hereden de esta.
    /// </summary>
    public class Entity2D
    {
        PlayerIndex mPlayerIndex;
        Animation mAnimation;
        Vector2 mPosition;
        float mRotation;

        public Entity2D()
        {
            mPlayerIndex = PlayerIndex.One;//Default
            mPosition = new Vector2(0, 0);
            mRotation = 0;
        }
        public Entity2D(PlayerIndex playerIndex,Animation animation)
        {
            mPlayerIndex = playerIndex;
            mAnimation = animation;
            mPosition = new Vector2(0, 0);
            mRotation = 0;
        }
        public float UpdateThumbStickAngle(Vector2 stick)
        {
            float angle = 0.0f;
            stick.Normalize();
            if ((stick.X >= 0 || stick.X <= 0) || (stick.Y >= 0 || stick.Y <= 0))
            {
                angle = (float)Math.Acos(stick.Y);
                if (stick.X < 0.0f)
                    angle = -angle;
            }
            return angle;
        }
        public void Update(GameTime gameTime)
        {
            mPosition.X += Input.GetGamePad((int)mPlayerIndex).ThumbSticks.Left.X * 5;
            mPosition.Y -= Input.GetGamePad((int)mPlayerIndex).ThumbSticks.Left.Y * 5;

            if (UpdateThumbStickAngle(Input.GetGamePad((int)mPlayerIndex).ThumbSticks.Right) != 0)
                mRotation = UpdateThumbStickAngle(Input.GetGamePad((int)mPlayerIndex).ThumbSticks.Right);
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(mAnimation.GetTexture(), mPosition, mAnimation.GetCurrentRect(),
               Color.White, mRotation, mAnimation.GetOrigin(), 1, SpriteEffects.None, 0);
        }
    }
}
